package com.zsoft.webmail.servlet.hainannet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.zsoft.webmail.*;
import com.zsoft.webmail.folder.*;
import comm.http.upload.SmartUpload;
import java.util.zip.*;
import com.zsoft.webmail.Application;
import com.zsoft.webmail.mimeutility.MimeUtility;
import comm.util.Utils;

public class FolderZipServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = Application.getProperty("ContentType");
  private static final String CONTENT_TYPE = "text/html";
  /**Initialize global variables*/
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    MemberAuthorization memberAuthToken=null ;
    comm.http.Request request1=new comm.http.Request(request,Application.getProperty("defaultInputCharset"));
    HttpSession session=request.getSession();
    Object object = session.getAttribute("authBean");
    if(object==null){
      session.setAttribute("authBean",new com.zsoft.webmail.AuthBean(session.getId(),request.getRemoteAddr()));
      response.sendRedirect( "login.jsp" );
      return;
    }else{
      memberAuthToken = ((AuthBean)object).getMemberAuthToken();
      if(memberAuthToken==null){
        response.sendRedirect( "login.jsp" );
        return;
      }
    }

    ListMsg list=new ListMsg();
    SmartUpload mailDownload =new SmartUpload();
        String folderid = request1.getParameter("boxid");

        try{
            folderid=comm.util.FileUtils.getFilename(folderid);
          list.listMsg(request,folderid);
        }catch(Exception e){}
	String userPath=memberAuthToken.homePath;
	char sep='/';
	String msgPath;
        String webPath=memberAuthToken.webPath;

        FolderList fl=null;
        try{
          fl=new FolderList(userPath+"/directory.cfg");
        }catch(Exception e){}

        msgPath = userPath+fl.getValueById(folderid);

        String foldername=fl.getFolderName(folderid);
        String tempzip=webPath+sep+folderid+".zip";

        try {
          FileOutputStream f = new FileOutputStream(tempzip);
          ZipOutputStream out1 = new ZipOutputStream(new DataOutputStream(f));
          try{
          out1.putNextEntry(new ZipEntry(folderid+"/"));

          for(int i=0;i<list.getMsgCount();i++) {
            String filename=list.getMsgInfo(i,"MessageName");
            DataInputStream in =new DataInputStream(new FileInputStream(msgPath+sep+filename));
            try{
            out1.putNextEntry(new ZipEntry(folderid+"/"+filename+".eml"));

            int blockSize = 4096;
            byte buf[] = new byte[blockSize];
            int bytesRead = 0;
            while((bytesRead = in.read(buf)) != -1)
              out1.write(buf, 0, bytesRead);

	    filename=null;
	    }finally{
            in.close();
            in=null;
            }

          }
          }finally{
          out1.close();
          f.close();
          }

          //--start to download temp zip file

          // Initialization
          mailDownload.initialize(super.getServletConfig(),request,response);

          // Download file with a physical path
          mailDownload.downloadFile(tempzip,MimeUtility.getContentType(
          comm.util.FileUtils.getFilename(tempzip)),new String(
          comm.util.FileUtils.getFilename(tempzip).getBytes(),"ISO-8859-1") );

          try{
          java.io.File tempfile = new java.io.File(tempzip);
          tempfile.delete();
          }catch(Exception e){}


        } catch(Exception e) {

        }


  }
  /**Process the HTTP Post request*/
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
  /**Clean up resources*/
  public void destroy() {
  }
}